clear all
set printcolor gs1, permanently
set more off
global dirin  ""
cd "$dirin"
global dirout ""
global dirinedomex ""

use Puebla_weekly_introcash_analysis.dta, clear
gen ban=(all_dates>=21157)

tsset id all_dates
bysort id: gen temp1=all_dates if _n==1
bysort id: egen cohort=max(temp1)
gen tenure=((all_dates-cohort)) +1
drop if all_dates<20821 
collapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates  (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban)

egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)

gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp

gen t1=(max_all_dates-min_all_dates)/7 +1
order id ban max_tenure min_tenure t1

gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)

save temp2.dta, replace

use temp2.dta, clear
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp

bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban

capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
drop min_epaid_trips

tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.

format cohort %td
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)

gen share_cash_ban=share_cash*ban
egen time=group(ban)

tsset id time
gen d_ln_mean_trips=d.ln_mean_trips
gen d_ln_mean_trip_credit=d.ln_mean_trip_credit
gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen p_mean_trip_credit=(mean_trip_credit-l.mean_trip_credit)/l.mean_trip_credit
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)


gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)

capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3

capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"
save temp3.dta, replace

		
		
use temp3.dta, clear
keep if flag_1==0
drop if p_mean_trips==.
drop if share_cash<0.01
drop if share_cash>.99
capture drop share_cash_bins
gen share_cash_bins=autocode(share_cash,100,0,1)
su share_cash_bins, d
tab share_cash_bins, g(share_)
keep dh_mean_trips share_* ln_total_fares_bf_ban cohort
reghdfe dh_mean_trips share_2-share_98 ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)	
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N
		gen share=_n
		drop if share==N 
		replace share=share/100  + .01
		
		set obs 98
		replace results1 = 0 in 98
		replace results2 = 0 in 98
		replace share= 0.01 in 98
		drop N
		drop if share==.
		rename results1 dh_mean_trip_credit
		cap drop c1 c2
		gen c1=dh_mean_trip_credit + 1.96*sqrt(results2)
		gen c2=dh_mean_trip_credit - 1.96*sqrt(results2)
		

sort share
#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected dh_mean_trip_credit share,color(midblue) lwidth(thick))
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge)) xlabel(0[.1]1)  ylabel(-0.6[.2]0) 
legend(off)
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
graph export "$dirout/Figure10a.eps", replace


#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected dh_mean_trip_credit share,color(midblue) lwidth(thick))
(function y =  2* ( (1-x)^(1/-2) *(1 - 1.1*ln(1-x)/-2) - 1) / ( (1-x)^(1/-2) *(1 - 1.1*ln(1-x)/-2) - 1 +2)  
 ,  range(0.01 .73199)
lcolor(cranberry) lwidth(thick) lpattern( longdash_dot  ))  
(function y =  2* ( (1-x)^(1/-4) *(1 - 1.1*ln(1-x)/-4) - 1) / ( (1-x)^(1/-4) *(1 - 1.1*ln(1-x)/-4) - 1 +2)  
 ,  range(0.01 .927)
 lcolor(black) lwidth(thick) lpattern( longdash_dot  ))  
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge)) xlabel(0[.1]1)  ylabel(-0.6[.2]0) 
legend(order (3 "{&eta} = 3" 4 "{&eta} = 5") ring(0) position(8) bmargin(medlarge) cols(1) size(large) region(lwidth(none)))
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
 graph export "$dirout/FigureH1.eps", replace
 
 
 
use Puebla_weekly_introcash_analysis.dta, clear
drop id new_user temp_all_dates
rename owner_uuid client_uuid

replace epaid_fares_usd=. if epaid_fares_usd==0
replace cash_fares_usd=. if cash_fares_usd==0
drop if epaid_fares_usd==. & cash_fares_usd==.
drop if epaid_fares_usd==0 & cash_fares_usd==0
drop if cash_trips==0 & epaid_trips==0
drop if cash_trips==. & epaid_trips==.

bysort client: egen min_all_dates=min(all_dates)
gen tenure = (21157-min_all_dates)
replace tenure=tenure/7
replace tenure=tenure + 1
bysort client_uuid: egen mean_tenure=mean(tenure)
drop if all_dates>21157 
drop if all_dates<20821  

collapse (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd (mean) mean_tenure, by(client_uuid)
rename mean_tenure tenure
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen share_cash_fares=cash_fares_usd/total_fares
gen share_cash_trips=cash_trips/total_trips
save temp.dta, replace


use Puebla_weekly_introcash_analysis.dta, clear
drop id new_user temp_all_dates
rename owner_uuid client_uuid
drop if all_dates<=21157  
keep client_uuid 
duplicates drop
merge 1:1 client_uuid using temp.dta
drop if _merge==1

keep if total_trips>10

gen survivied=(_merge==3)

gen lntenure=ln(tenure)
gen lnhist_fares=ln(total_fares/tenure)
gen lnhist_trips=ln(total_trips/tenure)

capture drop cash_share_fares_bins
gen cash_share_fares_bins=autocode(share_cash_fares,101,-.01,1)
su cash_share_fares_bins, d

tab cash_share_fares_bins, g(cash_share_fares_bins_)

reghdfe survivied cash_share_fares_bins_2-cash_share_fares_bins_101 lnhist_fares, absorb(i.tenure)  vce(robust)
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N 
		gen share=_n
		drop if share==N 
		replace share=share/100 
		drop if share>1
		set obs 101
		replace results1 = 0 in 101
		replace results2 = 0 in 101
		replace share= 0 in 101
		drop N
		drop if share==.
		rename results1 prob_survival
		cap drop c1 c2
		gen c1=prob_survival + 1.96*sqrt(results2)
		gen c2=prob_survival - 1.96*sqrt(results2)

sort share
#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected prob_survival share,color(midblue) lwidth(thick))
, ytitle(" ", height(5)) title(" ") ytitle(Probability of staying after ban, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge))  xlabel(0[.1]1) ylabel(-0.6[.2].1)  
legend(off)
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
graph export "$dirout/Figure10b.eps", replace





foreach weeks in 8 40 {
use Puebla_weekly_introcash_analysis.dta, clear
gen ban=(all_dates>=21157)
drop if all_dates>=21157 +7*`weeks'

tsset id all_dates
bysort id: gen temp1=all_dates if _n==1
bysort id: egen cohort=max(temp1)
gen tenure=((all_dates-cohort)) +1

drop if all_dates<20821  
collapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates  (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban)

egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)

gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp

gen t1=(max_all_dates-min_all_dates)/7 +1
order id ban max_tenure min_tenure t1

gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)

save temp2_shortrun_`weeks'.dta, replace

use temp2_shortrun_`weeks'.dta, clear
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban
capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
drop min_epaid_trips

tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.
format cohort %td
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)

gen share_cash_ban=share_cash*ban
egen time=group(ban)

tsset id time
gen d_ln_mean_trips=d.ln_mean_trips
gen d_ln_mean_trip_credit=d.ln_mean_trip_credit
gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen p_mean_trip_credit=(mean_trip_credit-l.mean_trip_credit)/l.mean_trip_credit
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)

gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)

capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3

capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"

save temp3_shortrun_`weeks'.dta, replace

	
use temp3_shortrun_`weeks'.dta, clear

keep if flag_1==0
drop if p_mean_trips==.
drop if share_cash<0.01
drop if share_cash>.99
capture drop share_cash_bins
gen share_cash_bins=autocode(share_cash,100,0,1)
su share_cash_bins, d
tab share_cash_bins, g(share_)

keep dh_mean_trips share_* ln_total_fares_bf_ban cohort p_mean_trips d_ln_mean_trips

reghdfe dh_mean_trips share_2-share_98 ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)

	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N
		gen share=_n
		drop if share==N 
		replace share=share/100  + .01
		
		set obs 98
		replace results1 = 0 in 98
		replace results2 = 0 in 98
		replace share= 0.01 in 98
		drop N
		drop if share==.
		rename results1 dh_mean_trip_credit
		cap drop c1 c2
		gen c1=dh_mean_trip_credit + 1.96*sqrt(results2)
		gen c2=dh_mean_trip_credit - 1.96*sqrt(results2)
		
gen weeks=`weeks'
 
save temp3_shortrun_`weeks'.dta, replace

}




use temp3_shortrun_8.dta, clear
append using temp3_shortrun_40.dta
save temp3_shortrun_all.dta, replace
sort share
#delimit
twoway 
(connected dh_mean_trip_credit share if weeks==8, color(cranberry*.3) lwidth(thick))
(connected dh_mean_trip_credit share if weeks==40, color(midblue*.3) lwidth(thick))
(fpfit dh_mean_trip_credit share if weeks==8, color(cranberry) lwidth(thick))
(fpfit dh_mean_trip_credit share if weeks==40, color(midblue) lwidth(thick))
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge)) xlabel(0[.1]1)  ylabel(-0.6[.2]0) 
legend(order(3 "After 2 Months" 4 "After 10 Months") region(lwidth(none))) 
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
graph export "$dirout/FigureD3.eps", replace

 
 

use Puebla_weekly_introcash_analysis.dta, clear
gen ban=(all_dates>=21157)
tsset id all_dates
bysort id: gen temp1=all_dates if _n==1
bysort id: egen cohort=max(temp1)
gen tenure=((all_dates-cohort)) +1
drop if all_dates<20821 
collapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates  (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban)
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp
gen t1=(max_all_dates-min_all_dates)/7 +1
order id ban max_tenure min_tenure t1
gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)
save temp2.dta, replace

use temp2.dta, clear
tsset id ban
bysort id: gen N=_N
gen flag_0=(N<2)
keep if flag_0==0
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban
capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
drop min_epaid_trips
tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.
format cohort %td
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)
gen share_cash_ban=share_cash*ban
egen time=group(ban)

tsset id time
gen d_ln_mean_trips=d.ln_mean_trips
gen d_ln_mean_trip_credit=d.ln_mean_trip_credit
gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen p_mean_trip_credit=(mean_trip_credit-l.mean_trip_credit)/l.mean_trip_credit
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)

gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)

capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3

capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"
save temp3b.dta, replace

		
		
use temp3b.dta, clear
keep if flag_1==0
drop if p_mean_trips==.
drop if share_cash<0.01
drop if share_cash>.99
capture drop share_cash_bins
gen share_cash_bins=autocode(share_cash,100,0,1)
su share_cash_bins, d
tab share_cash_bins, g(share_)
keep dh_mean_trips share_* ln_total_fares_bf_ban cohort
reghdfe dh_mean_trips share_2-share_98 ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)	
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N
		gen share=_n
		drop if share==N 
		replace share=share/100  + .01
		set obs 98
		replace results1 = 0 in 98
		replace results2 = 0 in 98
		replace share= 0.01 in 98
		drop N
		drop if share==.
		rename results1 dh_mean_trip_credit
		cap drop c1 c2
		gen c1=dh_mean_trip_credit + 1.96*sqrt(results2)
		gen c2=dh_mean_trip_credit - 1.96*sqrt(results2)

sort share

#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected dh_mean_trip_credit share,color(midblue) lwidth(thick))
(function y =  2* ( (1-x)^(1/-4) *(1 - 1.1*ln(1-x)/-4) - 1) / ( (1-x)^(1/-4) *(1 - 1.1*ln(1-x)/-4) - 1 +2)  
 ,   range(0.01 .895)
lcolor(cranberry) lwidth(thick) lpattern( longdash_dot  ))  
(function y =  2* ( (1-x)^(1/-7) *(1 - 1.1*ln(1-x)/-7) - 1) / ( (1-x)^(1/-7) *(1 - 1.1*ln(1-x)/-7) - 1 +2)  
 ,  range(0.01 .98)
 lcolor(black) lwidth(thick) lpattern( longdash_dot  ))  
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge)) xlabel(0[.1]1)  ylabel(-0.4[.2]0) 
legend(order (3 "{&eta} = 5" 4 "{&eta} = 8") ring(0) position(8) bmargin(medlarge) cols(1) size(large) region(lwidth(none)))
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
graph export "$dirout/FigureH3a.eps", replace


cd "$dirinedomex"
use "EDOMEX.dta", clear
duplicates drop
split week_date, p("-")
destring week_date1 week_date2 week_date3, replace
gen all_dates=mdy(week_date2, week_date3, week_date1)
gen temp_all_dates=all_dates
format %d all_dates
drop week_date*
egen id=group(client_uuid)
gen ban=(all_dates<20779)
tsset id all_dates
bysort id: gen temp1=all_dates if _n==_N
bysort id: egen cohort=max(temp1)
gen tenure=(cohort-all_dates) +1
drop if all_dates<20415  
drop if all_dates>21143  
gcollapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates ///
 (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban) fast
 
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp
gen t1=(max_all_dates-min_all_dates)/7 +1
gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)
save temp2.dta, replace


use temp2.dta, clear
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban
capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
drop min_epaid_trips
tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.
format cohort %td
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)
gen share_cash_ban=share_cash*ban
egen time=group(ban)

tsset id time
gen d_ln_mean_trips=d.ln_mean_trips
gen d_ln_mean_trip_credit=d.ln_mean_trip_credit

gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen p_mean_trip_credit=(mean_trip_credit-l.mean_trip_credit)/l.mean_trip_credit

gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)

gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)

capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3

capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"
save temp3.dta, replace
		
		
use temp3.dta, clear
keep if flag_1==0
drop if p_mean_trips==.
drop if share_cash<0.01
drop if share_cash>.99
capture drop share_cash_bins
gen share_cash_bins=autocode(share_cash,100,0,1)
su share_cash_bins, d
tab share_cash_bins, g(share_)
keep dh_mean_trips share_* ln_total_fares_bf_ban cohort
reghdfe dh_mean_trips share_2-share_98 ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N
		gen share=_n
		drop if share==N 
		replace share=share/100  + .01
		
		set obs 98
		replace results1 = 0 in 98
		replace results2 = 0 in 98
		replace share= 0.01 in 98
		drop N
		drop if share==.
		rename results1 dh_mean_trip_credit
		cap drop c1 c2
		gen c1=dh_mean_trip_credit + 1.96*sqrt(results2)
		gen c2=dh_mean_trip_credit - 1.96*sqrt(results2)
sort share


#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected dh_mean_trip_credit share,color(cranberry) lwidth(thick))
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash after introduction, size(vlarge)) xlabel(0[.1]1)  ylabel(-1.2[.2]0) 
legend(off)
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
graph export "$dirout/FigureH2.eps", replace





use "EDOMEX.dta", clear
duplicates drop
split week_date, p("-")
destring week_date1 week_date2 week_date3, replace
gen all_dates=mdy(week_date2, week_date3, week_date1)
gen temp_all_dates=all_dates
format %d all_dates
drop week_date*
egen id=group(client_uuid)
gen ban=(all_dates<20779)
tsset id all_dates
bysort id: gen temp1=all_dates if _n==_N
bysort id: egen cohort=max(temp1)
gen tenure=(cohort-all_dates) +1
drop if all_dates<20415  
drop if all_dates>21143  
gcollapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates ///
 (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban) fast
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp
gen t1=(max_all_dates-min_all_dates)/7 +1
gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)
save temp_TBD4.dta, replace

use temp_TBD4.dta, clear
bysort id: gen N=_N
gen flag_0=(N<2)
keep if flag_0==0
format cohort %td
gen flag_10 = (share_cash == 0 | share_cash == . | share_cash==1) 
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban
capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
drop min_epaid_trips

tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)
gen share_cash_ban=share_cash*ban
egen time=group(ban)
tsset id time
gen d_ln_mean_trips=d.ln_mean_trips
gen d_ln_mean_trip_credit=d.ln_mean_trip_credit
gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen p_mean_trip_credit=(mean_trip_credit-l.mean_trip_credit)/l.mean_trip_credit
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)

gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)
gen temp_trips_bfban = total_trips if ban==0
bysort id: egen min_total_trips_bfban=min(temp_trips_bfban)
gen temp_trips_afterban = total_trips if ban==1
bysort id: egen min_total_trips_afterban=min(temp_trips_afterban)

gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3

capture drop flag_4
gen flag_4=0
replace flag_4=1 if min_total_trips_bfban<11

capture drop flag_5
gen flag_5=0
replace flag_5=1 if min_total_trips_bfban<5

capture drop flag_6
gen flag_6=0
replace flag_6=1 if min_total_trips_afterban<11

capture drop flag_7
gen flag_7=0
replace flag_7=1 if min_total_trips_afterban<5

capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"

 
keep if flag_1==0
 drop if p_mean_trips==.
drop if share_cash<0.01
drop if share_cash>.99
capture drop share_cash_bins
gen share_cash_bins=autocode(share_cash,100,0,1)
su share_cash_bins, d
tab share_cash_bins, g(share_)
keep dh_mean_trips share_* ln_total_fares_bf_ban cohort d_ln_mean_trips flag*

save temp_TBD.dta, replace


use temp_TBD.dta, clear
keep if flag_1==0
reghdfe dh_mean_trips share_2-share_98 ln_total_fares_bf_ban, absorb(i.cohort)  vce(robust)
   
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.	
		gen N=_N
		gen share=_n
		drop if share==N 
		replace share=share/100  + .01
		
		set obs 98
		replace results1 = 0 in 98
		replace results2 = 0 in 98
		replace share= 0.01 in 98
		drop N
		drop if share==.
		rename results1 dh_mean_trip_credit
		cap drop c1 c2
		gen c1=dh_mean_trip_credit + 1.96*sqrt(results2)
		gen c2=dh_mean_trip_credit - 1.96*sqrt(results2)
sort share

#delimit
twoway 
(rarea c1 c2 share, pstyle(ci))
(connected dh_mean_trip_credit share,color(midblue) lwidth(thick))
(function y =  2* ( (1-x)^(1/-7) *(1 - 1.1*ln(1-x)/-7) - 1) / ( (1-x)^(1/-7) *(1 - 1.1*ln(1-x)/-7) - 1 +2)  
 ,  range(0.01 .98)
lcolor(cranberry) lwidth(thick) lpattern( longdash_dot  ))  
(function y =  2* ( (1-x)^(1/-11) *(1 - 1.1*ln(1-x)/-11) - 1) / ( (1-x)^(1/-11) *(1 - 1.1*ln(1-x)/-11) - 1 +2) 
 ,  range(0.01 .98)
 lcolor(black) lwidth(thick) lpattern( longdash_dot  ))  
, ytitle(" ", height(5)) title(" ") ytitle(Percent change trips, size(vlarge))
xtitle(Share of trips paid in cash before ban, size(vlarge)) xlabel(0[.1]1)  ylabel(-0.4[.2]0) 
legend(order (3 "{&eta} = 8" 4 "{&eta} = 12") ring(0) position(8) bmargin(medlarge) cols(1) size(large) region(lwidth(none)))
 graphregion(color(white)) plotregion(fcolor(white));
 #delimit cr
 graph export "$dirout/FigureH3b.eps", replace
  
  
 
